#include "mythread.h"
#include <QString>
#include <stdlib.h>


#define fifo2_path      "/home/debian/qt_test/fifo2"

extern uint8_t Rx_data[8];

union {
 unsigned char farray[4];
 int fa;
}utemp;

Mythread::Mythread(QObject *parent) : QThread(parent)
{

}


void Mythread::run()
{
    //判断fifo2是否存在  不存在则创建
    ret = access(fifo2_path, F_OK);
    if( ret < 0 ){
        ret = mkfifo(fifo2_path, O_RDWR );
        if( ret < 0 )
            printf("qt FIFO file creat error!\n");
    }
    fifo2_fd = open(fifo2_path, O_RDWR);
    if( fifo2_fd < 0 ){
        printf("qt FIFO2 file open error!\n");
        exit(-1);
    }
    while(1){
        if( read(fifo2_fd, Rx_data, 8) > 0 ){
            for(int i=0; i<8; i++){
                            printf("%x ",Rx_data[i]);
                        }
                        printf("\n");
            //判断包头 包尾及其校验
            if( Rx_data[0] == 0xa5 ){
                if( Rx_data[7] == 0x5a ){
                    if( Rx_data[6] == ((Rx_data[1]+Rx_data[2]+Rx_data[3]+Rx_data[4]+Rx_data[5])&0xff) ){
                        switch (Rx_data[1])
                        {
                        case 0x1:{
                            break;
                        }
                        case 0x2:{
                            break;
                        }
                        case 0x3:{
//                            bool ok;
//                            val_raw = Rx_data[2]+Rx_data[3]+Rx_data[4]+Rx_data[5];
                            for(int i=2; i<6; i++){
                                    printf("%x", Rx_data[i]);
                            }
                            printf("  <- qt data \n");
                            utemp.fa = 0.0;
                            utemp.farray[0] = Rx_data[5];
                            utemp.farray[1] = Rx_data[4];
                            utemp.farray[2] = Rx_data[3];
                            utemp.farray[3] = Rx_data[2];

                            for(int i=0; i<4; i++){
                                    printf("%x", utemp.farray[i]);
                            }

                            printf("  <- utemp data \n");
                            printf("%d\n",utemp.fa);
//                            adc_data_f = atoi(val_raw);
//                            adc_data_f = (float)((Rx_data[2]-'0')*1000+(Rx_data[3]-'0')*100+(Rx_data[3]-'0')*10+(Rx_data[4]-'0'));
//                            adc_data_f = adc_data_f/805.664;
//                            printf("adc_data_f = %f\n",adc_data_f);
//                            adc_data_f = (val_raw.toInt(&ok, 10) / 805.664);
//                            emit adc_task_signal(QString::asprintf("%s",adc_data_f));
                            emit adc_task_signal(QString("%1").arg(utemp.fa/805.664));

                            break;
                        }
                        default:
                            break;
                        }
                    }
                }
//                printf("222\n");
                QThread::msleep(300);
            }
        }
    }
}
